package com.gyms.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.classic.okhttp.beans.HVVenueBean;
import com.classic.okhttp.beans.HVVenueLabelBean;
import com.gyms.R;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import constant.PicSplit;
import glide.ImageLoaderUtils;
import utils.DecimalUtils;
import utils.SizeUtils;
import utils.StringUtils;

/**
 * ltx on 2016/11/4 13:17
 * 所有场馆适配器
 */
public class AllGymAdapter extends BaseAdapter {
    private List<HVVenueBean> mHVVenueBeans ;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private GymViewHolder mViewHolder;
    public static final int TYPE_TITLE = 0;//默认布局
    public static final int TYPE_COMPANY = 1;//空布局
    public AllGymAdapter(Context mContext) {
        this.mContext = mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setDataBeans(List<HVVenueBean> mHVVenueBeans) {
        this.mHVVenueBeans = mHVVenueBeans;
    }

    @Override
    public int getCount() {
        return null != mHVVenueBeans ? mHVVenueBeans.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mHVVenueBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public int getItemViewType(int position) {
        if (mHVVenueBeans.get(position).isCanAdd) {
            return TYPE_TITLE;
        } else {
            return TYPE_COMPANY;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        switch (getItemViewType(position)){
            case TYPE_TITLE:
                if(null==convertView){
                  convertView=   mLayoutInflater.inflate(R.layout.gym_divider_view, parent, false);
                }

                break;

                case TYPE_COMPANY:
                    if (null == convertView) {
                        convertView = mLayoutInflater.inflate(R.layout.all_gym_item, parent, false);
                        mViewHolder = new GymViewHolder(convertView);
                        convertView.setTag(mViewHolder);
                        //对于listview，注意添加这一行，即可在item上使用高度
                        AutoUtils.autoSize(convertView);
                    } else
                        mViewHolder = (GymViewHolder) convertView.getTag();

                    HVVenueBean mHVVenueBean = mHVVenueBeans.get(position);
                    if (!StringUtils.isNull(mHVVenueBean)) {
                        initData(mHVVenueBean);
                    }

                    break;
        }


        return convertView;
    }


    private void initData(HVVenueBean mHVVenueBean) {
        boolean isCollection=mHVVenueBean.getIsCollection();//收藏
        boolean isRecommend=mHVVenueBean.getIsRecommend();//推荐
        if(!isCollection&&!isRecommend){
            mViewHolder.mIvCollect.setVisibility(View.GONE);
        }else{
            mViewHolder.mIvCollect.setVisibility(View.VISIBLE);
        }
        if (isRecommend) {
            mViewHolder.mIvCollect.setImageResource(R.mipmap.icon_best);
        }
        if (isCollection) {//收藏大于推荐
            mViewHolder.mIvCollect.setImageResource(R.mipmap.icon_save_subscript);
        }
        mViewHolder.mTvGymName.setText(StringUtils.CS(mHVVenueBean.getVenueName()));
        mViewHolder.mTvGymDes.setText(StringUtils.CS(mHVVenueBean.getShortAddress()));
        Double mPrice=null;
       /* if(!StringUtils.isNull(mHVVenueBean.getMinPrice())){
            mPrice = mHVVenueBean.getMinPrice();
        }*/
        /*场馆列表中，非线上预订场馆，没有价格按钮*/
        if (StringUtils.isNull(mPrice)) {
            mViewHolder.mTvPrice.setVisibility(View.GONE);
        } else {
            mViewHolder.mTvPrice.setVisibility(View.VISIBLE);
            mViewHolder.mTvPrice.setText(DecimalUtils.setPriceStyle(mContext,mPrice,R.dimen.price_text_size_14,R.dimen.price_text_size_12,true));
        }
        ImageLoaderUtils.display(mContext, mViewHolder.mIvGym, mHVVenueBean.getVenueUrl());
        if (!StringUtils.isNull(mHVVenueBean.getDistance())) {
            double dis = mHVVenueBean.getDistance() / 1000;
            showDis(dis);
        }else{
            mViewHolder.mTvGymDis.setText("");
        }
        if(!StringUtils.isNull(mHVVenueBean.getVenueDiscount())){
            mViewHolder.mLlGymDiscount.setVisibility(View.GONE);
            String discountUrl=mHVVenueBean.getVenueDiscount().get(0).getDiscountIcon();//优惠图片
            String mDiscountContent=mHVVenueBean.getVenueDiscount().get(0).getDiscountContent();//优惠文本
            if(!StringUtils.isNull(discountUrl)){//优惠的图片为null隐藏
                mViewHolder.mLlGymDiscount.setVisibility(View.VISIBLE);
                mViewHolder.mTvDiscount.setVisibility(View.VISIBLE);
                ImageLoaderUtils.display(mContext, mViewHolder.mTvDiscount, PicSplit.urlAdd(discountUrl,PicSplit.TYPE_TAB));
            }else {
                mViewHolder.mTvDiscount.setVisibility(View.GONE);

            }
            mViewHolder.mTvdistanceText.setText(StringUtils.CS(mDiscountContent));

        }else{//为空就隐藏
            mViewHolder.mLlGymDiscount.setVisibility(View.GONE);
//            mViewHolder.mTvDiscount.setVisibility(View.GONE);
        }
        List<HVVenueLabelBean> mHVVenueLabelBeans = mHVVenueBean.getVenueLabel();
        if(!StringUtils.isNull(mHVVenueLabelBeans)){//标签为null
            mViewHolder.mLLTabAdd.setVisibility(View.VISIBLE);
            mViewHolder.mLLTabAdd.removeAllViews();
            for (int pos = 0; pos < mHVVenueLabelBeans.size(); pos++) {
                //最多显示三个
                if (pos == 3) {//如果第三个就停止循环
                    break;
                }
                ImageView iv = new ImageView(mContext);
                LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, SizeUtils.dp2px(mContext, 15));

                if (pos != 0) {
                    ll.setMargins(SizeUtils.dp2px(mContext, 5), 0, 0, 0);
                }
                iv.setLayoutParams(ll);
                Glide.with(mContext).load(mHVVenueLabelBeans.get(pos).getDiscountIcon()).fitCenter().into(iv);
                mViewHolder.mLLTabAdd.addView(iv);
            }
        }else{
            mViewHolder.mLLTabAdd.setVisibility(View.GONE);
        }

    }

    /**
     * 小于1公里的，距离显示“<1km”；大于等于1公里，小于等于50公里的，距离显示“xx.xkm”；大于50公里的，距离显示“>50km”；
     *
     * @param dis 公里
     */
    private void showDis(double dis) {
        String disText;
        if (dis < 1) {
            mViewHolder.mTvGymDis.setText("<1km");
        } else if (dis >= 1 && dis <= 50) {
            disText=DecimalUtils.getDistaceText(dis);
            mViewHolder.mTvGymDis.setText(disText + "km");
        } else {
            mViewHolder.mTvGymDis.setText(">50km");
        }
    }


    static class GymViewHolder {
        @BindView(R.id.iv_gym)
        ImageView mIvGym;//场馆图片
        @BindView(R.id.tv_gym_name)
        TextView mTvGymName;//场馆名
        @BindView(R.id.tv_gym_des)
        TextView mTvGymDes;//描述
        @BindView(R.id.tv_gym_dis)
        TextView mTvGymDis;//距离
        @BindView(R.id.ll_tab_add)
        LinearLayout mLLTabAdd; //添加tab的layout
        @BindView(R.id.tv_discount)
        ImageView mTvDiscount;//优惠
        @BindView(R.id.iv_collect)
        ImageView mIvCollect;//是否收藏
        @BindView(R.id.tv_price)
        TextView mTvPrice;
        @BindView(R.id.ll_gym_discount)
        LinearLayout mLlGymDiscount;//优惠信息
        @BindView(R.id.tv_distance_text)
        TextView mTvdistanceText;//优惠文字
        GymViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
